home *** CD-ROM | disk | FTP | other *** search
/ IRIX 6.4 Applications 1997 August / SGI IRIX 6.4 Applications 1997 August.iso / dist / mmailp.idb / usr / lib / Zmail / bin / mailserver.z / mailserver
Encoding:
Text File  |  1997-01-22  |  4.0 KB  |  153 lines

  1. #!/bin/csh -fb
  2. # (The "-fb" might need to be changed to "-f" on some systems)
  3. #
  4. # Mailserver -- a simple MIME mailserver script.
  5. # Makes all files under a tree available for MIME-based retrieval.
  6. # By default, it sends them as the MIME type "application/octet-stream"
  7. # However, for a file named "x/y/foo.bar", you can specify a "right"
  8. # MIME content-type by putting it in the file "x/y/foo.bar.ct".
  9.  
  10. # In a distributed sendmail environment, this script can be installed with lines
  11. #    somewhat like the following two in /usr/lib/aliases:
  12. # mail-server: local-mail-server@some-single-machine
  13. # local-mail-server: "|/full/path/to/mailserver"
  14.  
  15. # By default the program uses "mail-server" as its local return address.
  16. #   and makes available all files under /usr/spool/ftp.
  17. # You might need or want to change the following parameters:
  18. set LOCALADDR=mail-server
  19. set ROOTDIR=/usr/spool/ftp
  20. set MAINTAINER=postmaster
  21. set METAMAILDIR=/usr/local/bin
  22. set LOGADDR=andrew@thumper.bellcore.com
  23. # If LOGADDR is the empty string, no logging is done.
  24. #
  25. # The real program begins here.
  26.  
  27. setenv PATH ${METAMAILDIR}:${PATH}
  28. rehash
  29. set FromName=""
  30. set Subject=""
  31. set TmpFile=/tmp/ms.$$
  32. set FOORAW=$<
  33. while ("$FOORAW" != "") 
  34. set FOO=(` echo "$FOORAW" | tr "[" "x"`)
  35. set BAR=($FOO)
  36. set BARLC=(`echo $FOO | tr A-Z a-z`)
  37. if ($BARLC[1] == "from:") then
  38.     if ("$FromName" == "") then
  39.         set FromName = ($BAR[2-])
  40.     endif
  41. else if ($BARLC[1] == "reply-to:") then
  42.     set FromName = ($BAR[2-])
  43. else if ($BARLC[1] == "subject:") then
  44.     set Subject = ($BAR[2-])
  45. endif
  46. set FOORAW=$<
  47. end
  48. # Now, stdin just has the body left, to do with as we please.
  49. # We choose to interpret the first line as the request, nothing more
  50. if ("$Subject" == "") then
  51.     set Subject=$<
  52. endif
  53.  
  54. if ("$FromName" == "") then
  55.     cat > $TmpFile <<!
  56. From: $LOCALADDR@`hostname`
  57. To: $MAINTAINER
  58. Subject: $Subject
  59.  
  60. The metamail mailserver script, installed locally as $LOCALADDR, 
  61. has received a request without any reply address.
  62.  
  63. It is possible that this is the result of a user running the "mailserver" 
  64. program by hand.  It is intended to be run as an automated recipient of 
  65. mail requests, rather than an interactive program.
  66.  
  67. No reply is being generated, but the contents of the request are 
  68. reproduced below.  If no message appears below, then this program was 
  69. probably run in some circumstance other than mail delivery.
  70. --------------------
  71. !
  72.     cat $TmpFile - | /usr/lib/sendmail $MAINTAINER
  73.     # Takes the rest of the message from standard input
  74.     rm $TmpFile
  75.     exit 0
  76. endif
  77.  
  78. set danger=`echo $Subject | fgrep ..`
  79. if ($danger != "") then
  80.     cat > $TmpFile <<!
  81. From: $LOCALADDR@`hostname`
  82. To: $FromName
  83. Subject: Re: $Subject
  84.  
  85. For security reasons, this mailserver automatically rejects all requests 
  86. that contain ".." in the path name.
  87.  
  88. The file you requested, if it exists, will not be sent to you.
  89. !
  90.     /usr/lib/sendmail -t < $TmpFile
  91.     rm $TmpFile
  92.     exit 0
  93. endif
  94.  
  95. cd $ROOTDIR
  96. if (! -e "$Subject") then
  97.     cat > $TmpFile <<!
  98. From: $LOCALADDR@`hostname`
  99. To: $FromName
  100. Subject: Re: $Subject
  101.  
  102. You recently sent mail to this mail-server requesting the file: 
  103.     $Subject
  104.  
  105. That file does not exist, so your request could not be met.
  106.  
  107. Here is a list of the currently available files:
  108. --------------------------------
  109. !
  110.     ls -R >> $TmpFile
  111.     /usr/lib/sendmail -t < $TmpFile
  112.     rm $TmpFile
  113.     exit 0
  114. endif
  115.  
  116. if (-e ${Subject}.ct) then
  117.     set ct=`cat ${Subject}.ct`
  118. else 
  119.     set ct="application/octet-stream"
  120. endif
  121.  
  122. metasend -b -t "$FromName" -f "$Subject" -m "$ct" -s "Re: $Subject"
  123. if ($status != 0) then
  124.     cat > $TmpFile <<!
  125. From: $LOCALADDR@`hostname`
  126. To: $FromName
  127. Subject: Re: $Subject
  128.  
  129. You recently sent mail to this mail-server requestion the file: 
  130.     $Subject
  131.  
  132. An unanticipated error apparently precluded delivery of the file.
  133. Please accept our apologies.
  134.  
  135. Command failed: 
  136.   metasend -b -t "$FromName" -f "$Subject" -m "$ct" -s "Re: $Subject"
  137.  
  138. !
  139.     /usr/lib/sendmail -t < $TmpFile
  140.     rm $TmpFile
  141.     exit 0
  142. endif
  143.  
  144. if ("$LOGADDR" != "") then
  145.     /usr/lib/sendmail -t <<!
  146. From: ${LOCALADDR}@`hostname`
  147. To: $LOGADDR
  148. Subject: Autosend delivery report
  149.  
  150. The file: $Subject 
  151. was sent to: $FromName
  152. !
  153. exit 0
  154.